package com.hpjpw.idoapp.controller;

import com.hpjpw.idoapp.domain.StudentImportVo;
import com.hpjpw.idoapp.domain.TeacherImportVo;
import com.hpjpw.idoapp.service.UserImportService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @BelongsProject: ruoyi
 * @BelongsPackage: com.hpjpw.idoapp.controller
 * @Author: hpj
 * @CreateTime: 2021-01-04 16:38
 * @Description: 学生与老师导入
 */
@Api("学生与老师导入")
@RestController()
@RequestMapping("/ido/user/import")
public class UserImportController {
    @Autowired
    UserImportService userImportService;

    @Autowired
    private TokenService tokenService;

    @ApiOperation("导入学生")
    @Log(title = "导入学生" , businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ido:student:add')")
    @PostMapping("/student/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<StudentImportVo> util = new ExcelUtil<StudentImportVo>(StudentImportVo.class);
        List<StudentImportVo> studentImportVos = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = userImportService.importStudentUser(studentImportVos, operName);
        return AjaxResult.success(message);
    }

    @ApiOperation("获取学生导入模板")
    @GetMapping("/student/importTemplate")
    public AjaxResult importTeacherTemplate() {
        ExcelUtil<StudentImportVo> util = new ExcelUtil<StudentImportVo>(StudentImportVo.class);
        return util.importTemplateExcel("学生导入模板");
    }

    @ApiOperation("导入老师")
    @Log(title = "导入老师" , businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ido:teacher:add')")
    @PostMapping("/teacher/importData")
    public AjaxResult importTeacherData(MultipartFile file) throws Exception {
        ExcelUtil<TeacherImportVo> util = new ExcelUtil<TeacherImportVo>(TeacherImportVo.class);
        List<TeacherImportVo> teacherImportVoList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = userImportService.importTeacherUser(teacherImportVoList, operName);
        return AjaxResult.success(message);
    }

    @ApiOperation("获取老师导入模板")
    @GetMapping("/teacher/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<TeacherImportVo> util = new ExcelUtil<TeacherImportVo>(TeacherImportVo.class);
        return util.importTemplateExcel("老师导入模板");
    }
}
